if (window.plus) {
    ready();
} else {
    document.addEventListener('plusready', ready, false);
}

$(function(){
  if(AGENT_CLIENT.WEB){
    ready();
  } 
});

function ready() {
    var thisPage = null,
        html = [],
        user = null,
        myScroll = null,
        pullUp = null,
        pullDown = null,
        query = {
            index: 1,
            size: 10,
            query: "",
            search: {},
            sorting: {}
        },
        thispage = null;

    var invitelistPage = function () {
        this.init();
    };

    invitelistPage.prototype = {
        init: function () {
            this.addScroll();
            this.initData();
            this.bindEvent();
        },
        initData: function () {
            user = getLocalStorage(STORAGE_MANAGER.user);
//          if (!user || !user.id) {
//              redirect("../account/login.html");
//              return false;
//          }
            this.loadInviteList();
        },
        addScroll: function () {
            //添加滚动条
            pullUp = $("#pullUp");
            pullDown = $("#pullDown");
            myScroll = new IScroll('#wrapper', {
                probeType: 2,
                scrollbars: true,
                mouseWheel: false,
                fadeScrollbars: true,
                bounce: true,
                interactiveScrollbars: false,
                shrinkScrollbars: 'scale',
                click: true,
                keyBindings: true,
                momentum: true
            });
            myScroll.on("scrollEnd", function () {
                if ((this.y < 0) && (this.y == this.maxScrollY)) {
                    console.log("滚动到底部");
                    pullUp.show();
                    setTimeout(function () {
                        query.index++;
                        thispage.loadInviteList("add");
                    }, 500);
                } 
                else if ((this.y == 0) && (this.y != this.maxScrollY)) {
                  console.log("滚动到顶部");
                  pullDown.show();
                  setTimeout(function () {
                      query.index = 1;
                      thispage.loadInviteList("renovate");
                  }, 500);
                }
            });
        },
        loadInviteList: function (action) {
            $.ajax({
                type: "POST",
                url: CONFIG_MANAGER.RequstUrl + "v1/member/invite/" + user.id,
                async: true,
                data: query,
                headers: {"token": user.token},
            }).done(function (data) {
                var data = JSON.parse(data), invist = data.data, i = 0, html = [];
                if(undefined == action &&  invist.length == 0 ){
                  $("#inviteList").html('<div class="zhanwushuju"><img src="../../images/zanwushuju.png"><br>暂无数据</div>');
                }else{
                  while (invist[i]) {
                    html.push('<div class="yqjlk mbbottom">');
                    html.push('<div class="ygjlbg">');
                    html.push('<p class="yqzccg">注册成功</p>');
                    html.push('<p class="yqhyny"><span>￥' + invist[i].money + '</span>好友：' + invist[i].nickname + '</p>');
                    html.push('<p class="yqhyny yqhyny1"><span>奖励至钱包余额</span>' + invist[i].createtime + '</p>');
                    html.push('</div>');
                    html.push('</div>');
                    i++;
                  }
                  if( "add" == action ){
                    $(html.join('')).appendTo($("#inviteList"));
                  }else{
                    $("#inviteList").html(html.join(''));
                  }
                }
                //加载完成后隐藏上拉下拉提示
                setTimeout(function () {
                    myScroll.refresh();
                    pullUp.hide();
                    pullDown.hide();
                }, 100);
            }).fail(function (data) {
                var infor = JSON.parse(data);
                $.alert(infor.message);
            });
        },
        bindEvent: function () {
          //回退
          $("#back").on("click", function () {
              back();
          });

        }
    };

    thispage = new invitelistPage();

}
